﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Diggins.Jigsaw;
using Domains;

namespace Parser
{
    public class Interpreter
    {
        public Interpreter(string path, string[] args)
        {

            var lst = new List<Value>();
            foreach (var s in args)
            {
                switch (s)
                {
                    case "false" :
                        {
                            lst.Add(new Bool(false));
                            break;
                        }
                    case "true" :
                        {
                            lst.Add(new Bool(true));
                            break;
                        }
                    case "0" :
                        {
                            lst.Add(new Num(0));
                            break;
                        }
                    case "1" :
                        {
                            lst.Add(new Num(1));
                            break;
                        }
                    default : throw new Exception("unsupported argument");
                }
            }

            var streamReader = new StreamReader(path);
            var text = streamReader.ReadToEnd();
            streamReader.Close();

            var nodes = FinalGrammar.Program.Parse(text);
            var state = new State() {Input = new Input(lst), Output = new Output(), Memory = new Memory()};
            var evaluator = new Evaluator3();

            var result = evaluator.Evaluate(nodes[0], state);
            foreach (var elem in state.Output.Value)
            {
                if (elem is Num) Console.WriteLine(((Num)elem).Value);
                if (elem is Bool) Console.WriteLine(((Bool)elem).Value);
            }
                
        }
    }
}
